
var robot = require("robotjs");
var $ = require('jquery')
const clipboard = require('electron').clipboard
var a, b, c, d, e, f, g, h, i, j, k
var JudgeString1, JudgeString2, JudgeString3, JudgeString4, JudgeString5, JudgeString6, JudgeString7, JudgeString8, JudgeString9, JudgeString10
var KeyIndex = 0
var NextKey = undefined
const hook = require('iohook')
var InputString = '1111111111111111'//所有输入过的字符
var arr = []
hook.on('keydown', (msg) => {
    KeyIndex++
    NextKey = msg.rawcode
    InputString += PressedChar(NextKey)
    if (NextKey == 32) {
        FormatString()
        judge()
    }
    // console.log(InputString)
})
hook.start();


var StartFlag = "m('"
var EndFlag = ")"
$(document).ready(function () {
    $('#textarea').keyup(function () {
        arr = []
        var start = false
        var StartIndex = undefined
        var str = ''
        $(this).val().split('\n').forEach((element, index) => {//先将所有的m函数整理出来
            if (element.substring(0, 3) == StartFlag) {//开始标志
                str = element
                StartIndex = index
                start = true
            } else if (index == StartIndex + 1) {
                str += ("['" + element + "',")
            } else if (element.substring(0) == EndFlag) {//结束标志
                str += ("]" + element)
                arr.push(str)
                str = ''
                start = false
                StartIndex = undefined
            } else if (start) {
                str += ("'" + element + "',")
            } else {
                arr.push(element)
            }
        });
        function unique(arr) {
            return Array.from(new Set(arr))
        }
        arr = unique(arr)
        console.log(arr)
    })
})
async function judge() {
    arr.forEach(element => {
        eval(element)
    });
}
function m(character, content) {
    backspace(character)
    if (JudgeString10 == character
        || JudgeString9 == character
        || JudgeString8 == character
        || JudgeString7 == character
        || JudgeString6 == character
        || JudgeString5 == character
        || JudgeString4 == character
        || JudgeString3 == character
        || JudgeString2 == character
    ) {
        let str = ''
        content.forEach(element => {
            console.log(element);
            str += element + '\n'
        });

        hook.stop()
        copy(str)
        // robot.keyTap("enter")
        hook.start()
    }
}

function copy(element) {
    clipboard.writeText(element)
    robot.keyTap("v", 'control')
}

function backspace(character) {
    if (JudgeString10 == character) {
        for (let i = 0; i < 11; i++) {
            hook.stop()
            robot.keyTap("backspace");
            hook.start()
        }
    } else if (JudgeString9 == character) {
        for (let i = 0; i < 10; i++) {
            hook.stop()
            robot.keyTap("backspace");
            hook.start()
        }
    } else if (JudgeString8 == character) {
        for (let i = 0; i < 9; i++) {
            hook.stop()
            robot.keyTap("backspace");
            hook.start()
        }
    } else if (JudgeString7 == character) {
        for (let i = 0; i < 8; i++) {
            hook.stop()
            robot.keyTap("backspace");
            hook.start()
        }
    } else if (JudgeString6 == character) {
        for (let i = 0; i < 7; i++) {
            hook.stop()
            robot.keyTap("backspace");
            hook.start()
        }
    } else if (JudgeString5 == character) {
        for (let i = 0; i < 6; i++) {
            hook.stop()
            robot.keyTap("backspace");
            hook.start()
        }
    } else if (JudgeString4 == character) {
        for (let i = 0; i < 5; i++) {
            hook.stop()
            robot.keyTap("backspace");
            hook.start()
        }
    } else if (JudgeString3 == character) {
        for (let i = 0; i < 4; i++) {
            hook.stop()
            robot.keyTap("backspace");
            hook.start()
        }
    } else if (JudgeString2 == character) {
        for (let i = 0; i < 3; i++) {
            hook.stop()
            robot.keyTap("backspace");
            hook.start()
        }
    }
}


function t(character, content, times, ctrlTimes) {//模板输出
    backspace(character)

    if (JudgeString10 == character
        || JudgeString9 == character
        || JudgeString8 == character
        || JudgeString7 == character
        || JudgeString6 == character
        || JudgeString5 == character
        || JudgeString4 == character
        || JudgeString3 == character
        || JudgeString2 == character
    ) {
        hook.stop()
        copy(content)
        hook.start()
        for (let i = 0; i < ctrlTimes; i++) {
            hook.stop()
            robot.keyTap("left", 'control')
            hook.start()
        }

        for (let i = 0; i < times; i++) {
            hook.stop()
            robot.keyTap("left");
            hook.start()
        }
    }
}
async function p(what) {
    if (JudgeString1 === what) {
        console.log(what)
        let c = await WaitKey()
        if (c == 32) {
            hook.stop()
            robot.keyTap('backspace')
            robot.keyTap('backspace')
            robot.keyTap('backspace')
            copy(what)
            hook.start()
        }
    }
}
function FormatString() {
    JudgeString1 = InputString.substring(InputString.length - 2, InputString.length - 1)
    JudgeString2 = InputString.substring(InputString.length - 3, InputString.length - 1)
    JudgeString3 = InputString.substring(InputString.length - 4, InputString.length - 1)
    JudgeString4 = InputString.substring(InputString.length - 5, InputString.length - 1)
    JudgeString5 = InputString.substring(InputString.length - 6, InputString.length - 1)
    JudgeString6 = InputString.substring(InputString.length - 7, InputString.length - 1)
    JudgeString7 = InputString.substring(InputString.length - 8, InputString.length - 1)
    JudgeString8 = InputString.substring(InputString.length - 9, InputString.length - 1)
    JudgeString9 = InputString.substring(InputString.length - 10, InputString.length - 1)
    JudgeString10 = InputString.substring(InputString.length - 11, InputString.length - 1)
}



async function WaitKey() {
    return await new Promise((resolve, reject) => {
        let LastKeyIndex = KeyIndex
        let timer = setInterval(() => {
            if (KeyIndex > LastKeyIndex) {
                clearInterval(timer)
                resolve(NextKey)
            }
        }, 10)
    })
}



function PressedChar(a) {
    let JudgeString1 = undefined
    if (a >= 65 && a <= 90) {
        JudgeString1 = String.fromCharCode(a + 32)
    }
    else if (a == 48) {
        JudgeString1 = ")"
    }
    else if (a == 49) {
        JudgeString1 = "!"
    }
    else if (a == 50) {
        JudgeString1 = "@"
    }
    else if (a == 51) {
        JudgeString1 = "#"
    }
    else if (a == 52) {
        JudgeString1 = "$"
    }
    else if (a == 53) {
        JudgeString1 = "%"
    }
    else if (a == 54) {
        JudgeString1 = "^"
    }
    else if (a == 55) {
        JudgeString1 = "&"
    }
    else if (a == 56) {
        JudgeString1 = "*"
    }
    else if (a == 57) {
        JudgeString1 = "("
    }
    else if (a == 186) {
        JudgeString1 = ":"
    }
    else if (a == 187) {
        JudgeString1 = "+"
    }
    else if (a == 188) {
        JudgeString1 = "<"
    }
    else if (a == 189) {
        JudgeString1 = "_"
    }
    else if (a == 190) {
        JudgeString1 = ">"
    }
    else if (a == 191) {
        JudgeString1 = "?"
    }
    else if (a == 192) {
        JudgeString1 = "~"
    }
    else if (a == 219) {
        JudgeString1 = "{"
    }
    else if (a == 220) {
        JudgeString1 = "|"
    }
    else if (a == 221) {
        JudgeString1 = "}"
    }
    else if (a == 222) {
        JudgeString1 = '""'
    } else {
        return String.fromCharCode(a)
    }
    return JudgeString1
}